package d.business
{
	import flash.external.ExternalInterface;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.messaging.channels.SecureAMFChannel;
	import mx.messaging.events.ChannelEvent;
	import mx.messaging.events.ChannelFaultEvent;
	
	public class dAMFChannel extends SecureAMFChannel
	{
		
		public function dAMFChannel(id:String=null, uri:String=null)
		{
			super(id, uri);
			addEventListener(ChannelEvent.DISCONNECT,channelDisconnectHandler);
			addEventListener(ChannelFaultEvent.FAULT,channelFaultHandler);
			requestTimeout = -1;
		}
				
		/*
		Session time out in IE https , dispatch ChannelFaultEvent-->ChannelDisconnectEvent-->ChannelFaultEvent
		*/						
		protected function channelDisconnectHandler(event:ChannelEvent):void
		{   			
			//stop event transfer , prevent further fault
			event.stopImmediatePropagation();	
		}
		
		private function channelFaultHandler(event:ChannelFaultEvent):void
		{ 			
			if(ExternalInterface.call("GetCookie","SMSESSION")=="LOGGEDOFF"){				
			    showSeccsionTimeOut();	
			    event.stopImmediatePropagation();
//			    event.stopPropagation();
			}else{
				Alert.show("Connection failed. Please refresh and try again.",
					"Warning Message",4, null, seccsionTimeOutHandler);
				event.stopImmediatePropagation();
			}
		}
		
		private function showSeccsionTimeOut():void{					
			Alert.show("Connection failed due to session time out. Please login and try again.",
				"Session Time Out",4, null, seccsionTimeOutHandler);
		}
		
		private function seccsionTimeOutHandler(event:CloseEvent):void {
			//rederect to login page
			//var ref:URLRequest = new URLRequest("javascript:location.reload(true)");
			var ref:URLRequest = new URLRequest(RoleUtils.getLoginPage());
			navigateToURL(ref, "_self");
		}  
	}
}